package com.windowes.weichang.service;

import com.baomidou.mybatisplus.plugins.Page;
import com.windowes.weichang.common.ServerResponse;
import com.windowes.weichang.dao.HistoryCustomerMapper;
import com.windowes.weichang.pojo.HistoryCustomer;
import com.windowes.weichang.utils.POIUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/***
 *
 * @author: yangqing
 * @email: qing.yang@bangdao-tech.com
 * @createTime: 2018/6/16 下午9:12
 * @description:
 *
 **/
@Service
@Slf4j
public class HistoryCustomerService {


    @Autowired
    HistoryCustomerMapper customerMapper;

    public ServerResponse importHistory(MultipartFile file) {
        try {
            List<String[]> strings = POIUtil.readExcel(file);
            List<HistoryCustomer> customers = new ArrayList<>();
            for (String[] ar : strings) {
                String join = StringUtils.join(ar, "|");
                if (StringUtils.isBlank(join) || join.contains("|||||||||||||") || join.contains("|||||||||||||")) {
                    continue;
                }
                //处理行内
                HistoryCustomer customer = getHistory(ar);
                if (customer != null && !StringUtils.isBlank(customer.getPhone())) {
                    customers.add(customer);
                }
            }

            return batchInsertCustomers(customers);
        } catch (IOException e) {
            log.error("错误是:{0}", e);
            return ServerResponse.createByErrorMessage("文件读取异常");
        }
    }

    @Transactional
    public ServerResponse batchInsertCustomers(List<HistoryCustomer> customers) {
        if (customers == null || customers.isEmpty()) {
            return ServerResponse.createByErrorMessage("未检测到数据,成功导入0条！");
        }
        int count = customerMapper.batchInsert(customers);
        return ServerResponse.createBySuccessMessage("成功导入" + count + "条!");
    }

    /**
     * @param ar
     * @return
     */

    public HistoryCustomer getHistory(String[] ar) {
        if (ar == null || ar.length < 2) {
            return null;
        }

        // 8月份|4|张先生|18502685325|佳平里6-5-401|4.3600000000000003|北辰区|2016.8.31|55|坚郎外开|双面灰|5+12+5|隐形|纱窗慧亮提供|慧亮门窗||
        HistoryCustomer customer = new HistoryCustomer();
        customer.setSheetName(ar[0]);
        for (int i = 2; i < ar.length; i++) {
            String val = ar[i];
            if (i == 2) {
                customer.setName(val);
            } else if (i == 3) {
                //联系方式为空,不录入
                if (StringUtils.isBlank(val) || val.equals("null")) {
                    return null;
                }
                customer.setPhone(val);
            } else if (i == 4) {
                customer.setAddress(val);
            } else if (i == 5) {
                customer.setArea(val);
            } else if (i == 6) {
                customer.setCity(val);
            } else if (i == 7) {
                if (!StringUtils.isBlank(val)) {
                    val = val.replace(".", "-");
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    try {
                        Date parse = sdf.parse(val);
                        customer.setCreateTime(parse);
                    } catch (ParseException e) {
                        log.error("时间录入有误, {}", e);
                    }
                }
            } else if (i == 8) {
                customer.setExtrudate(val);
            } else if (i == 9) {
                customer.setHardware(val);
            } else if (i == 10) {
                customer.setColor(val);
            } else if (i == 11) {
                customer.setGlass(val);
            } else if (i == 12) {
                customer.setRemark(val);
            } else if (i == 13) {
                customer.setInstallUser(val);
            }
        }

        if (ar.length == 15) {
            customer.setScreen(ar[13]);
            customer.setInstallUser(ar[14]);
        }
        if (ar.length == 17) {
            customer.setScreen(ar[12]);
            customer.setRemark(ar[13]);
            customer.setInstallUser(ar[14]);
        }
        return customer;
    }

    public ServerResponse users(String keyword,int pageNo, int pageSize) {
        Page<HistoryCustomer> page = new Page<>(pageNo, pageSize);
        page.setRecords(customerMapper.selectByPage(page, keyword));
        return ServerResponse.createBySuccess(page);
    }
}
